package com.kun.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.index.entity.Blog;
import com.kun.index.entity.Comment;
import com.kun.index.mapper.BlogMapper;
import com.kun.index.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 博客表 服务实现类
 * </p>
 *
 * @author kunkun
 * @since 2024-02-28
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Autowired
    private BlogMapper blogMapper;


    @Override
    public Map<String,Object> queryBlogs(Map map) {
        HashMap<String, Object> resultMap = new HashMap<>();
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        if (map.get("queryTitleLike")!=null){
            wrapper.like("title",(String)map.get("queryTitleLike"));
        }
        if (map.get("auditStatue")!=null){
            wrapper.eq("auditStatue",(Integer)map.get("auditStatue"));
        }
        if (map.get("queryLabelId")!=null){
            Integer labelId= (Integer) map.get("queryLabelId");
            wrapper.inSql("id","select blogId from blog_label where labelId = "+labelId);
        }
        if ( map.get("orderByUpdateTime")!=null && (Boolean) map.get("orderByUpdateTime")){
            wrapper.orderByDesc("updateTime");
        }
        if (map.get("pageIndex")!=null&&map.get("pageSize")!=null){
            int pageIndex = (Integer) map.get("pageIndex");
            int pageSize = (Integer) map.get("pageSize");

            Page<Blog> page = new Page<>(pageIndex,pageSize);
            blogMapper.selectPage(page, wrapper);
            long total =  page.getTotal();
            List<Blog> blogs = page.getRecords();
            resultMap.put("totalBlogNum",total);
            resultMap.put("blogs",blogs);
        }else {
            List<Blog> blogs = blogMapper.selectList(wrapper);
            resultMap.put("blogs",blogs);
        }
        return resultMap;
    }

    @Override
    public int addBlog(Blog blog) {
        blogMapper.insert(blog);
        return blog.getId();
    }

    @Override
    public int updateBlog(Blog blog) {
        return blogMapper.updateById(blog);
    }

    @Override
    public int deleteBlog(Blog blog) {
        return blogMapper.deleteById(blog.getId());
    }

    @Override
    public int batchDeleteBlog(List<Integer> ids) {
        return blogMapper.deleteBatchIds(ids);
    }

    @Override
    public Blog queryBlogById(Integer id) {

        return blogMapper.selectById(id);
    }

    @Override
    public List<Blog> queryBlogsByIds(ArrayList<Integer> blogIds) {
        ArrayList<Blog> blogs = new ArrayList<>();
        for (Integer blogId : blogIds) {
            Blog blog = this.queryBlogById(blogId);
            blogs.add(blog);
        }
        return blogs;
    }

    @Override
    public Integer addBlogHitsNum(Integer id) {
        Blog blog = this.queryBlogById(id);
        Integer num= blog.getHitsNum()+1;
        blog.setHitsNum(num);
        this.updateBlog(blog);
        return num;
    }


}
